package com.kun.rental.controller;

import com.kun.rental.entity.Dept;
import com.kun.rental.service.IDeptService;
import com.kun.rental.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  部门管理
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Slf4j
@RestController
@RequestMapping("/rental/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;


    //查询部门列表及子列表
    @PostMapping("/list")
    public Result<List<Dept>> list(@RequestBody Dept dept){
        return Result.success(deptService.selectList(dept));
    }


    /**
     * 选择上级部门接口
     * @return
     */
    @GetMapping("/selectTree")
    public Result tree(){
        return Result.success().setData(deptService.selectTree());
    }


    /**
     * 新增功能
     * @param dept
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Dept dept){
        return deptService.save(dept) ? Result.success() : Result.fail();
    }

    /**
     * 修改功能
     * @param dept
     * @return
     */

    @PutMapping("/update")
    public Result update(@RequestBody Dept dept){
        return deptService.updateById(dept) ? Result.success() : Result.fail();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return deptService.removeById(id)?Result.success():Result.fail();
    }

    /**
     * 判断是否有子部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result hasChildren(@PathVariable Integer id){
        return deptService.hasChildren(id)?
                Result.success().setMessage("有"):Result.success().setMessage("无");
    }


}
